package com.mayn.common;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.config.Plugins;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.mayn.jst.shop.model.ShopAccountModel;
import com.mayn.jst.shop.model.ShopModel;
import com.mayn.util.FileUtil;

public class JfinalInit {

	private static final Logger LOG = LoggerFactory.getLogger(JfinalInit.class);

	public static String defaultConfFile = "dbconfig.txt";
	public static String defaultLog4jFile = "log4j.properties";

	public static String getConfigFile() {
		// 加载配置,默认加载src/main/resources下的配置,可自定义到conf目录下
		try {
			String configPath = JfinalInit.getConfigPath();
			String dbConfFilePath = configPath + defaultLog4jFile;
			if (FileUtil.exist(dbConfFilePath)) {
				return dbConfFilePath;
			}
		} catch (Exception e) {
			LOG.info("config load error!");
		}
		return "";
	}

	public static void InitDb(Boolean isWeb, Plugins me) {

		setLog4jConfigFile(defaultLog4jFile);
		String configPath = getConfigPath();

		String dbConfFile = "";
		if (new File(configPath + defaultConfFile).exists()) {
			dbConfFile = configPath + defaultConfFile;
			PropKit.use(new File(dbConfFile));
		} else {
			dbConfFile = defaultConfFile;
			PropKit.use(dbConfFile);
		}

		LOG.info("config load success from " + dbConfFile);
		// 聚石塔RDS数据库配置
		String jdbcUrl_jst = PropKit.get("jdbcUrl_jst");
		String user_jst = PropKit.get("user_jst");
		String password_jst = PropKit.get("password_jst");
		if (jdbcUrl_jst.length() > 0) {
			C3p0Plugin c3p0Plugin_jst = new C3p0Plugin(jdbcUrl_jst, user_jst, password_jst);
			ActiveRecordPlugin arp_jst = new ActiveRecordPlugin("jst", c3p0Plugin_jst);
			if (!isWeb) {
				// 非web环境需要start插件
				c3p0Plugin_jst.start();
				arp_jst.start();
			} else {
				if (me != null) {
					me.add(c3p0Plugin_jst);
					me.add(arp_jst);
				}
			}

		}
		// API数据库配置(默认)
		String jdbcUrl = PropKit.get("jdbcUrl");
		String user = PropKit.get("user");
		String password = PropKit.get("password");
		if (jdbcUrl.length() > 0) {
			C3p0Plugin c3p0Plugin = new C3p0Plugin(jdbcUrl, user, password);
			ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);

			// 数据表影射
			arp.addMapping("shop", ShopModel.class);
			arp.addMapping("shop_account", ShopAccountModel.class);

			if (!isWeb) {
				// 非web环境需要start插件
				c3p0Plugin.start();
				arp.start();
			} else {
				if (me != null) {
					me.add(c3p0Plugin);
					me.add(arp);
				}
			}

		}

		// ERP数据库配置
		String jdbcUrl_erp = PropKit.get("jdbcUrl_erp");
		String user_erp = PropKit.get("user_erp");
		String password_erp = PropKit.get("password_erp");
		String jdbcDriver_erp = PropKit.get("jdbcDriver_erp");
		if (jdbcUrl_erp.length() > 0) {
			C3p0Plugin c3p0Plugin_erp = new C3p0Plugin(jdbcUrl_erp, user_erp, password_erp, jdbcDriver_erp);
			ActiveRecordPlugin arp_erp = new ActiveRecordPlugin("erp", c3p0Plugin_erp);
			if (!isWeb) {
				// 非web环境需要start插件
				c3p0Plugin_erp.start();
				arp_erp.start();
			} else {
				if (me != null) {
					me.add(c3p0Plugin_erp);
					me.add(arp_erp);
				}
			}
		}

		if (!isWeb) {
			LOG.info("非Web环境运行Jfinal...");
		}
	}

	public static String getStartPath() {
		String startPath = null;
		try {
			startPath = URLDecoder.decode(JfinalInit.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (startPath.endsWith(".jar")) {// 可执行jar包运行的结果里包含".jar"
			// 截取路径中的jar包名
			startPath = startPath.substring(0, startPath.lastIndexOf("/") + 1);
		}
		return startPath;
	}

	public static String getConfigPath() {
		String startPath = getStartPath();
		String extraPath = "conf/";
		return startPath + extraPath;
	}

	public static void setLog4jConfigFile(String log4jFile) {
		try {
			String startPath = getStartPath();
			String configPath = getConfigPath();
			String log4jFilePath = configPath + log4jFile;
			String defLog4jFilePath = startPath + log4jFile;
			if (new File(log4jFilePath).exists()) {
				PropertyConfigurator.configure(log4jFilePath);
				LOG.debug("Load log4j properties file : " + log4jFilePath);
			} else if (new File(defLog4jFilePath).exists()) {
				PropertyConfigurator.configure(defLog4jFilePath);
				LOG.debug("Load log4j properties file : " + log4jFilePath);
			} else {

			}
			LOG.info("This Program start at path : " + startPath);
		} catch (Exception e) {
			LOG.error("config load error!");
		}
	}

}
